﻿using nplJobsCommon.JobSeeker;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace nplJobsDAL.Select
{
    public class SelectSavedJobByParam : DataAccessBase
    {
        public List<SavedJob> SavedJob { get; set; }
        public string JobTitle { get; set; }
        public string Location { get; set; }
        public int Deadline { get; set; }
        public Guid JobSeekerId{ get; set; }

        public SelectSavedJobByParam()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectSavedJobsByParam.ToString();
        }

        #region GetSavedJobToDisplay
        public List<SavedJobToDisplay> GetSavedJobToDisplay()
        {
            DataBaseHelper dbHelper;
            DataSet dsSavedJobToDisplay;
            List<SavedJobToDisplay> lst = new List<SavedJobToDisplay>();
            try
            {
                SelectSavedJobParameters selectProfessionalInfoParameters = new SelectSavedJobParameters(this.JobTitle, this.Location, this.Deadline, this.JobSeekerId);
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dbHelper.Parameters = selectProfessionalInfoParameters.Parameters;
                dsSavedJobToDisplay = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);
                lst= GetList(dsSavedJobToDisplay);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return lst;
        }
        #endregion

        #region SQL parameters for selecting saved job info by params
        public class SelectSavedJobParameters
        {
            public string JobTitle { get; set; }
            public string Location { get; set; }
            public int Deadline { get; set; }
            public Guid JobSeekerId { get; set; }

            private SqlParameter[] _parameters;

            public SelectSavedJobParameters(string jobTitle, string location, int deadline, Guid jobSeekerId)
            {
                this.JobTitle = jobTitle;
                this.Location = location;
                this.Deadline = deadline;
                this.JobSeekerId = jobSeekerId;

                Build();
            }
            private void Build()
            {
                SqlParameter[] parameters = 
                {
                    new SqlParameter("@JobTitle",  this.JobTitle),
                    new SqlParameter("@Location", this.Location),
                    new SqlParameter("@Deadline", this.Deadline),
                    new SqlParameter("@JobseekerID",  this.JobSeekerId),
                };

                Parameters = parameters;
            }

            public SqlParameter[] Parameters
            {
                get { return _parameters; }
                set { _parameters = value; }
            }
        }
        #endregion

        #region GetList
        private List<SavedJobToDisplay> GetList(DataSet dsSavedJobToDisplay)
        {
            List<SavedJobToDisplay> savedJobToDisplay;
            try
            {
                savedJobToDisplay = (from n in dsSavedJobToDisplay.Tables[0].AsEnumerable()
                                select new SavedJobToDisplay
                                   {
                                       SavedJobID = n.Field<Int64>("ID"),
                                       JobID = n.Field<Int64>("JobID"),
                                       JobSeekerID = n.Field<Guid>("JobSeekerID"),
                                       CompanyImage = n.Field<string>("CompanyImage"),
                                       ApplicationDateline = n.Field<int>("ApplicationDateline"),
                                       JobTitle = n.Field<string>("JobTitle"),
                                       Location = n.Field<string>("Location"),
                                       CompanyName = n.Field<string>("CompanyName"),
                                       PostedDate = n.Field<DateTime>("PostedDate")
                                   }).ToList();

                return savedJobToDisplay;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        }
        #endregion
    }
}
